package com.dong.sortingAlgorithm;
/*
二分法
1.设置一个中位数，且中位数一直在循环里面变化，一直二分
 */
public class BinarySearch {
    public static void main(String[] args) {
        int[] num= {-1,0,3,5,9,12};
        int target = 9;
        boolean b = searchBinary(num, target);
        System.out.println(b);
    }

    public static boolean searchBinary(int[] nums,int target){
        int low =0;
        int high = nums.length-1 ;
        while (low<=high){
            int mid = (low+high)/2;
            int k = nums[mid];
            if (k == target){
                return true;
            }
            else if (k > target){
                high = mid -1;
            }
            else {
                low = mid + 1;
            }
        }
        return false;
    }


}
